package com.smile.weather2.network;

import com.smile.weather2.network.api.NetWorkApi;
import com.smile.weather2.url.Urls;

import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * 网络请求类
 * Created by smile on 2016/8/26.
 */
public class NetWork {
    private static NetWorkApi netWorkApi;

    private static OkHttpClient okHttpClient = new OkHttpClient();
    private static ScalarsConverterFactory scalarsConverterFactory = ScalarsConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();

    public static NetWorkApi getNetWorkApi(){
        if(netWorkApi == null){
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Urls.BASE_URL)
                    .addConverterFactory(scalarsConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            netWorkApi = retrofit.create(NetWorkApi.class);
        }

        return netWorkApi;
    }

}
